<template>
    <div>
        <el-form :model="form" label-width="80px">
            <el-form-item label="账号">
                <el-input v-model="form.username"/>
            </el-form-item>
            <el-form-item label="密码">
                <el-input v-model="form.password"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submit">
                    登陆
                </el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data(){
            return {
                form:{}
            }
        },
        methods:{
            submit(){
                let formData = new FormData();
                for (let key in this.form) {
                    formData.append(key, this.form[key]);
                }
                this.$http.post("http://localhost:8081/api/login", formData).then(res => {
                    if (res.data.code === 0) {
                        this.$message({message:res.data.msg, type: 'success'});
                        this.$http.defaults.headers.common['Authorization'] = res.data.access_token;
                        this.$router.push("/");
                    }else{
                        this.$message.error(res.data.msg);
                    }
                }).catch(e => {
                    this.$message.error(e.message);
                })
            }
        }

    }
</script>
